package cn.cnooc.config;



import java.io.Serializable;

/**
 * Created on 2019/4/21.
 *
 * @author awh
 */
public class ResultInfo<T> implements Serializable {

    private String msg;

    private int ret;

    private T data;

    public ResultInfo() {
        super();
    }

    private ResultInfo(T data){
        this.ret = CodeMsg.SUCCESS.getRet();
        if(data instanceof String){
            this.msg = String.valueOf(data);
        }else{
            this.msg = "成功";
            this.data = data;
        }
    }

    private ResultInfo(T data, String message){
        this.ret = CodeMsg.SUCCESS.getRet();
        this.msg = message;
        this.data = data;
    }

    private ResultInfo(CodeMsg cm, T data, String message){
        this.ret = cm.getRet();
        this.msg = message;
        this.data = data;
    }

    private ResultInfo(CodeMsg cm) {
        if(cm == null){
            return;
        }
        this.ret = cm.getRet();
        this.msg = cm.getMsg();
    }

    private ResultInfo(Integer ret, String message) {
        this.ret = ret;
        this.msg = message;
    }
    /**
     * 成功时候的调用,传入data
     * @return
     */
    public static <T> ResultInfo<T> success(T data){
        return new ResultInfo<T>(data);
    }

    /**
     * 成功时候的调用,传入CodeMsg
     * @param cm
     * @param <T>
     * @return
     */
    public static <T> ResultInfo<T> success(CodeMsg cm){
        return new ResultInfo<T>(cm);
    }
    /**
     * 成功，不需要传入参数
     * @return
     */
    public static <T> ResultInfo<T> success(){
        return (ResultInfo<T>) success("");
    }
    /**
     * 成功时候的调用,自定义message
     * @return
     */
    public static <T> ResultInfo<T> success(T data,String message){
        return new ResultInfo<T>(data,message);
    }

    /**
     * 成功时候的调用,自定义message，自定义code
     * @return
     */
    public static <T> ResultInfo<T> success(CodeMsg cm,T data,String message){
        return new ResultInfo<T>(cm,data,message);
    }
    /**
     * 成功时候的调用,扩展消息参数
     * @param cm
     * @param msg
     * @return
     */
    public static <T> ResultInfo<T> success(CodeMsg cm,String msg){
        cm.setMsg(msg);
        return new ResultInfo<T>(cm);
    }
    /**
     * 失败时候的调用
     * @return
     */
    public static <T> ResultInfo<T> error(CodeMsg cm){
        return new ResultInfo<T>(cm);
    }
    /**
     * 失败时候的调用,扩展消息参数
     * @param cm
     * @param msg
     * @return
     */
    public static <T> ResultInfo<T> error(CodeMsg cm,String msg){
        cm.setMsg(msg);
        return new ResultInfo<T>(cm);
    }
    public static <T> ResultInfo<T> error(Integer cm,String msg){
        return new ResultInfo<T>(cm,msg);
    }

    public T getData() {
        return data;
    }
    public String getMsg() {
        return msg;
    }
    public int getRet() {
        return ret;
    }

}
